import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

import re
import datastore


class MainPage(webapp.RequestHandler):

  def Write(self,t):
    self.response.out.write(t)

  def Writeln(self,t):
    self.response.out.write(t)
    self.response.out.write("<br>")

  def get(self):

    self.Writeln( "clearing.. ")

    nl = "<br>"

    dbsel  =  self.request.get('db') 

    query = ""
    if( dbsel == "all"):   
       query = "select __key__ from TournyPlayer"    
    elif ( dbsel != ""):
       query = "select __key__ from TournyPlayer where tournament='" + str(dbsel) + "'"
    else:
       print("/clean?db=#####")
       return

    self.Writeln( query)
    keys = db.GqlQuery(query)

    for k in keys:
     t = db.get(k)
     self.Writeln("deleting "+ str(t.tournament) + " " + str(t.fname) + " " + str(t.lname) )
     t.delete()


application = webapp.WSGIApplication(
                                     [('/clean', MainPage)],
                                     debug=True)



def main():
    run_wsgi_app(application)

if __name__ == "__main__":

#   print __file__
#   print __name__

    main()




